/**
 * 折叠树
 */
function menu_tree(){
	$('table.main_table tbody tr').addClass('treetr'); //消除奇偶行色
	$("#main_tbody tr").each(function(){
		$('.child-of-'+$(this).attr('id')).hide();//有子树的隐藏
		var padding_l = 30+$(this).children('td').children('input[type=checkbox]').attr('level')*20;
		$(this).children().attr("style","text-align:left; padding-left:"+padding_l+"px; font-size:15px;");
		if($('.child-of-'+$(this).attr('id')).length>0){//有孩子的父树加class
			$(this).addClass('parent');			
			$(this).children('td').prepend("<span class='collapsed' style='padding-left:30px;'></span>")
			.children('span').click(function(){
			var toggleClass=$(this).attr('class');
			if(toggleClass == 'collapsed'){		
				var chickid = $(this).parents('tr').attr('id');
				$('.child-of-'+chickid).fadeIn('fast');
				$(this).removeClass('collapsed');
				$(this).addClass('expanded');
			} else if(toggleClass == 'expanded'){
				var chk = $("input[type='checkbox']");
				var count = chk.length;
				var num = chk.index($(this).siblings('input'));
				var level_bottom = chk.eq(num).attr('level');
				for(var j = num+1;j<count;j++){
					var le = chk.eq(j).attr('level');
					if(eval(le)>eval(level_bottom)) 
					{
					  $('#main_tbody tr').eq(j).fadeOut('fast').children('td').children('span').removeClass('expanded').addClass('collapsed');					 
					}
					else if(eval(le) == eval(level_bottom)) break;	
				}
				$(this).removeClass('expanded');
				$(this).addClass('collapsed');
			}	
				});
		}
	});
}

/**
 * 选择框
 */
function checknode(obj)
{
    var chk = $("input[type=checkbox]");
    var count = chk.length;
    var num = chk.index(obj);
    var level_top = level_bottom =  chk.eq(num).attr('level');
    for (var i=num; i>=0; i--)
    {
            var le = chk.eq(i).attr('level');
            if(eval(le) < eval(level_top)) 
            {
                chk.eq(i).attr("checked",true);
                var level_top = level_top-1;
            }
    }
    for (var j=num+1; j<count; j++)
    {	
            var le = chk.eq(j).attr('level');
            if(chk.eq(num).attr("checked")=="checked") {
                if(eval(le) > eval(level_bottom)) chk.eq(j).attr("checked",true);
                else if(eval(le) == eval(level_bottom)) break;
            }
            else {
                if(eval(le) > eval(level_bottom)) chk.eq(j).attr("checked",false);
                else if(eval(le) == eval(level_bottom)) break;
            }
    }
}